<template>
  <basic-container>
    <avue-crud
      ref="crud"
      v-model="form"
      :page.sync="page"
      :data="tableData"
      :table-loading="tableLoading"
      :option="tableOption"
      @on-load="getList"
      @search-change="searchChange"
      @search-reset="searchReset"
    >
      <template slot="menuLeft">
        <el-button
          style="margin-right: 20px"
          type="primary"
          size="small"
          icon="el-icon-plus"
          @click="add('')"
        >
          新增套餐
        </el-button>
      </template>
      <template
        slot="menu"
        slot-scope="scope"
      >
        <el-button
          type="text"
          @click="toDetail(scope.row.id)"
        >
          详情
        </el-button>
        <el-button
          v-if="scope.row.status === COMBO_STATUS.DOWN"
          type="text"
          @click="add(scope.row.id)"
        >
          编辑
        </el-button>
        <el-button
          v-if="scope.row.status === COMBO_STATUS.UP"
          type="text"
          @click="handleCombo(scope.row)"
        >
          下架
        </el-button>

        <el-button
          v-if="scope.row.status === COMBO_STATUS.DOWN"
          type="text"
          @click="handleCombo(scope.row)"
        >
          上架
        </el-button>
      </template>
    </avue-crud>
    <addComboDialog
      :id="id"
      v-model="addVisible"
      :detail="isDetail"
      @onSuccess="getList(page, true)"
    ></addComboDialog>
  </basic-container>
</template>
<script>
import { tableOption } from '@/const/crud/pet/cloudAdoptionCombo.js';
import crudMixins from '@/mixins/crud.js';
import addComboDialog from './components/addComboDialog.vue';
import { fetchPackList, updatePackStatus } from '@/api/core/adopt';
import { COMBO_STATUS } from '@/const/type';
export default {
  components: { addComboDialog },
  mixins: [crudMixins],
  data() {
    return {
      COMBO_STATUS,
      tableOption: tableOption,
      form: {},
      addVisible: false,
      id: '',
      isDetail: false,
    };
  },
  methods: {
    getList(page, refresh) {
      if (refresh) {
        this.page.current = 1;
        this.page.currentPage = 1;
      }
      this.tableLoading = true;
      fetchPackList(
        Object.assign(
          {
            current: this.page.currentPage,
            size: this.page.pageSize,
          },
          this.searchForm
        )
      )
        .then((res) => {
          this.tableData = res.data.data.records;
          this.page.total = res.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    add(id) {
      this.id = id;
      this.isDetail = false;
      this.addVisible = true;
    },
    toDetail(id) {
      this.id = id;
      this.isDetail = true;
      this.addVisible = true;
    },
    //下架或上架
    handleCombo(row) {
      const pkInfo = `${row.cycleNumber}${
        row.cycleUnit === '1' ? '个月' : '年'
      }`;
      const tip = `你确认要${
        row.status === COMBO_STATUS.UP ? '下架' : '上架'
      }<span style="color:#409EFF">${
        row.$type
      }·${pkInfo}</span>的云认养服务产品吗？`;
      this.$confirm(tip, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        dangerouslyUseHTMLString: true,
      })
        .then(() => {
          return updatePackStatus(
            row.id,
            row.status === COMBO_STATUS.UP ? COMBO_STATUS.DOWN : COMBO_STATUS.UP
          );
        })
        .then(() => {
          this.$message.success('操作成功');
          this.getList();
        });
    },
  },
};
</script>
